import Vue from 'vue'
import VueRouter from 'vue-router'




const Longin =()=>import('views/longin')

const Home = () =>import('views/home')
const Welcome= () =>import('components/welcome')
const User= () =>import('components/user')
const Roles= () =>import('components/permissions/roles')
const Right= () => import('components/permissions/right')
const GoodCats= () => import('components/goodsadmin/goodscats')
const GoodList= () => import('components/goodsadmin/goodslist')
const GoodParams= () => import('components/goodsadmin/goodsparams')
const Add= () => import('components/goodsadmin/add')
const Order= () => import('components/order/order')
const Report =() =>import('components/report/report')


Vue.use(VueRouter)

  const routes = [
    {
      path:'/',
      redirect: '/longin'
    },
    {
      path:'/longin',
      component:Longin
    },
    {
      path:'/home',
      component:Home,
      redirect: '/welcome',
      children:[
        {
          path:'/welcome',
          component:Welcome
        },{
          path:'/users',
          component:User
        },{
          path:'/roles',
          component:Roles
        },{
          path:'/rights',
          component:Right
        },{
          path:'/categories',
          component:GoodCats
        },{
          path:'/goods',
          component:GoodList
        },{
          path:'/params',
          component:GoodParams
        },{
          path:'/orders',
          component:Order
        },{
          path:'/add',
          component:Add
        },{
          path:'/reports',
          component:Report
        }

      ]
    }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //to表示将要访问的路径
  //from 表示重那个路径跳转而来
  //next() 放行  next()  next('路径')  强制跳转的路径

  if(to.path!=='/home')  next()
  if(to.path==='/home'){
    const token=window.sessionStorage.getItem('token')
    if(!token) return next('/longin')
    next()
  }
})

export default router
